package com.example.makerspace.controller;


import com.example.makerspace.dto.Result;
import com.example.makerspace.utils.FileUtils;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.List;

@Controller
@ResponseBody
@Slf4j
public class CommonController {
  private  final  static  String   testHttpPrefix="https://119.45.199.13:3035/static/";

   private  final  static  String  devHttpPrefix="https://nism.ytcata.com:443/static/";

    @PostMapping("/api/videoList")
    public Result videoList() {
        File file = new File(devHttpPrefix+ "videos/");
        List<String> videos = Lists.newArrayList();
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            for (File f : files) {
                String name = f.getName();
                videos.add(devHttpPrefix + "videos/" + name);
            }
        }
        return Result.success(videos);
    }

    @GetMapping("/api//download")
    public void download(HttpServletResponse response, @RequestParam String filePath) {
        FileUtils.downloadFile(response, devHttpPrefix + filePath);
    }



}

